package com.qwc.blog.controller.back;

import com.qwc.blog.entity.User;
import com.qwc.blog.service.IUserService;
import com.qwc.blog.service.impl.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Collection;

/**
 * @author qwc
 * @Date 2021/10/16 11:41
 */
@RestController
@RequestMapping("test")
@Slf4j
public class TestController {
    @Autowired
    private IUserService userService;

    @Autowired
    private CaffeineCacheManager cacheManager;

    @Autowired
    private TestService testService;

    private LocalTime now1;
    private LocalTime now2;

    @GetMapping("/user")
    public User user() {
        return userService.getUser("1");
    }

    @GetMapping("/getCacheName")
    public Collection getCacheName() {
        Collection<String> cacheNames = cacheManager.getCacheNames();
        return cacheNames;
    }

    @GetMapping("/cache/{id}/{value}")
    public String cache(@PathVariable String id, @PathVariable String value) {
        String test = testService.cacheTest(id, value);
        now2 = LocalTime.now();
        log.info("cache->{}", this.now2);
        return test;
    }

    @GetMapping("/getCache/{id}")
    public Object getCache(@PathVariable String id) {
        Cache cache = cacheManager.getCache("cache");
        Cache.ValueWrapper valueWrapper = cache.get(id);
        LocalTime now = LocalTime.now();
        log.info("cache->{}", now2);
        log.info("cache->{}", Duration.between(this.now2, now).toMillis());
        return valueWrapper.get();
    }

    @GetMapping("/getAllCache")
    public Object getAllCache() {
        Cache cache = cacheManager.getCache("cache");
        com.github.benmanes.caffeine.cache.Cache<Object, Object> cacheq = (com.github.benmanes.caffeine.cache.Cache) cache.getNativeCache();
        return cacheq.asMap();
    }

    @GetMapping("/cacheTest2/{key}/{value}")
    public String cacheTest2(@PathVariable String key, @PathVariable String value) {
        testService.cacheTest2(key, value);
        now1 = LocalTime.now();
        log.info("codeCache->{}", this.now1);
        return "ok";
    }

    @GetMapping("/getCache2/{key}")
    public String getCache2(@PathVariable String key) {
        LocalTime now = LocalTime.now();
        log.info("codeCache->{}", now);
        log.info("codeCache->{}", Duration.between(this.now1, now).toMillis());
        return testService.getCache2(key);
    }

    public static void main(String[] args) {
        LocalDate now = LocalDate.now();
        LocalDate localDate = now.minusMonths(1L);
        System.out.println(now + "_" + localDate);
    }
}
